package com.estar.assistsurvy.utils;

import com.estar.assistsurvy.activity.ForgetPwdActivity;
import com.estar.assistsurvy.activity.LoginActivity;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.DialogInterface.OnClickListener;
import cn.jpush.android.api.JPushInterface;

public class ExitUtils {
	
	private static SharedPreferences userPrefer;
	private static SharedPreferences rolePrefer;
	private static Intent intent;

	public static void ExitApp(Context context){
		int currentVersion = android.os.Build.VERSION.SDK_INT; 
        if (currentVersion > android.os.Build.VERSION_CODES.ECLAIR_MR1) { 
            Intent startMain = new Intent(Intent.ACTION_MAIN); 
            startMain.addCategory(Intent.CATEGORY_HOME); 
            startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
            context.startActivity(startMain); 
            System.exit(0); 
//        } else {// android2.1 
//            ActivityManager am = (ActivityManager) context.getSystemService(ACTIVITY_SERVICE); 
//            am.restartPackage(context.getPackageName()); 
        } 
	}
	
	public static void Unlogin(final Activity activity,String message){
		rolePrefer=activity.getSharedPreferences("role", Context.MODE_PRIVATE);
		userPrefer=activity.getSharedPreferences("user", Context.MODE_PRIVATE);
		new AlertDialog.Builder(activity, AlertDialog.THEME_HOLO_LIGHT)
		.setMessage(message)
		.setNegativeButton("退出", new OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				userPrefer.edit().clear().commit();
				rolePrefer.edit().clear().commit();
				JPushInterface.setAliasAndTags(activity.getApplicationContext(), "", null,null);
//				ExitApp(activity);
				intent=new Intent(activity, LoginActivity.class);
				activity.startActivity(intent);
				activity.finish();
			}
		})
		.setPositiveButton("重新登录", new OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				userPrefer.edit().clear().commit();
				rolePrefer.edit().clear().commit();
				JPushInterface.setAliasAndTags(activity.getApplicationContext(), "", null,null);
				intent=new Intent(activity, LoginActivity.class);
				activity.startActivity(intent);
				activity.finish();
			}
		})
		.create()
		.show();
	}
}
